<?php
function getImageInfo($path){
	$info = getimagesize($path);
	$data['width'] = $info[0];
	$data['height'] = $info[1];
	$data['type'] = $info['mime'];
	return $data;

}
function openImg($path,$type){
     switch ($type) {
     	case 'image/jpeg':
     	case 'image/jpg':
     	case 'image/pjpeg':
     		 $img = imagecreatefromjpeg($path);
     		break;
     	case 'image/png':
     	case 'image/x-png':
     	      $img = imagecreatefrompng($path);
     		break;
     	case 'image/gif':
     	      $img = imagecreatefromgif($path);
     		break;
     	case 'image/wbmp':
     	      $img = imagecreatefromwbmp($path);
     	    break;  
     	default:
     	      exit('图片类型不支持');
     		
     }
  return $img;
}

function water($img,$water,$pos=9,$tm=100){//位置默认9 透明度默认100
     $info = getImageInfo($img);//调用自定义函数得到图片信息。
     $logo = getImageInfo($water);
     
     $dst = openImg($img,$info['type']);//调用自定义函数。得到相应类型的图片资源
     $src = openImg($water,$logo['type']);

     switch ($pos) {
          case 1:
               $x=0;
               $y=0;
               break;
          case 2:
               $x=ceil(($info['width']-$logo['width'])/2);
               $y=0;
               break;
          case 3:
               $x=$info['width']-$logo['width'];
               $y=0;
               break;
          case 4:
               $x=0;
               $y=ceil(($info['height']-$logo['height'])/2);
               break;
          case 5:
               $x=ceil(($info['width']-$logo['width'])/2);
               $y=ceil(($info['height']-$logo['height'])/2);
               break;
          case 6:
               $x=$info['width']-$logo['width'];
               $y=ceil(($info['height']-$logo['height'])/2);
               break;
          case 7:
               $x=0;
               $y=$info['height']-$logo['height'];
               break;
          case 8:
               $x=ceil(($info['width']-$logo['width'])/2);
               $y=$info['height']-$logo['height'];
               break;
          case 9:
               $x=$info['width']-$logo['width'];
               $y=$info['height']-$logo['height'];
               break;
          case 0:
          
          default:
               $x=mt_rand(0,$info['width']-$logo['width']);//mt_rand()函数表示从参数范围内得到一个随机数
               $y=mt_rand(0,$y=$info['height']-$logo['height']);
               break;
     }
     imagecopymerge($dst, $src, $x, $y, 0, 0, $logo['width'], $logo['height'], $tm);
     imagejpeg($dst);
     imagedestroy($dst);
     imagedestroy($src);


}
header('Content-type:image/jpeg');
water('fbb.png','du.png',0,50);